package demo1;
//浅拷贝
public class Person implements Cloneable{
    public String name;
    public int age;

    public Money m = new Money();

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public Object clone()
            throws CloneNotSupportedException {
        //return super.clone();  -->  浅拷贝的代码

        /*  深拷贝的代码
                ^
                |        */
        Person tmp = (Person) super.clone();
        tmp.m = (Money) this.m.clone();//需要向下转型的原因：super.clone() & this.m.clone() 的类型都是Object
        return tmp;
    }
}
